package com.example.demo.utils;

import com.aliyun.cloudauth20190307.models.Id2MetaVerifyRequest;
import com.aliyun.cloudauth20190307.models.Id2MetaVerifyResponse;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.example.demo.enums.RealNameAuthenticationEnums;
import com.example.demo.mapper.AdminMapper;
import com.example.demo.mapper.RealNameAuthenticationMapper;
import com.example.demo.mapper.UserMapper;
import com.example.demo.pojo.Admin.Admin;
import com.example.demo.pojo.RealNameAuthentication;
import com.example.demo.pojo.User.User;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

import static com.example.demo.config.Id2MetaVerifyConfig.id2MetaVerifyAutoRoute;

@Component
public class UserUtil {
    @Resource
    private UserMapper userMapper;

    @Resource
    private AdminMapper adminMapper;

    @Resource
    private RealNameAuthenticationMapper realNameAuthenticationMapper;

    /**
     * 获取当前登录用户id
     *
     * @param userName 用户名
     * @return 登录用户id
     */
    public Integer getUserId(String userName) {
        QueryWrapper<User> userQueryWrapper = new QueryWrapper<>();
        QueryWrapper<User> user = userQueryWrapper.eq("user_name", userName);
        return userMapper.selectOne(user).getId();
    }

    /**
     * 获取当前登录用户id
     *
     * @param userName 用户名
     * @return 登录用户id
     */
    public Integer getAdminId(String userName) {
        QueryWrapper<Admin> adminQueryWrapper = new QueryWrapper<>();
        QueryWrapper<Admin> admin = adminQueryWrapper.eq("username", userName);
        return adminMapper.selectOne(admin).getId();
    }

    /**
     * 获取用户真实姓名通过用户id
     */
    public String getRealNameById(Integer userId) {
        QueryWrapper<User> userQueryWrapper = new QueryWrapper<>();
        QueryWrapper<User> user = userQueryWrapper.eq("id", userId);
        return userMapper.selectOne(user).getRealName();
    }

    /**
     * 获取登录用户真实姓名
     *
     * @param userName 用户名
     * @return 真实姓名
     */
    public String getRealName(String userName) {
        QueryWrapper<User> userQueryWrapper = new QueryWrapper<>();
        QueryWrapper<User> user = userQueryWrapper.eq("user_name", userName);
        return userMapper.selectOne(user).getRealName();
    }

    /**
     * 获取登录用户权限
     *
     * @param userName 用户名
     * @return 用户权限
     */
    public String getRole(String userName) {
        QueryWrapper<User> userQueryWrapper = new QueryWrapper<>();
        QueryWrapper<User> user = userQueryWrapper.eq("user_name", userName);
        return userMapper.selectOne(user).getRole();
    }

    public String getAdminRole(String userName) {
        QueryWrapper<Admin> adminQueryWrapper = new QueryWrapper<>();
        adminQueryWrapper.eq("username", userName);
        return adminMapper.selectOne(adminQueryWrapper).getRole();
    }

    /**
     * 判断用户是否实名认证
     *
     * @param userId 用户id
     */
    public boolean getRealNameAuthentication(Integer userId) {
        QueryWrapper<RealNameAuthentication> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", userId);
        int status = realNameAuthenticationMapper.selectOne(queryWrapper).getRealNameStatus();
        return status == RealNameAuthenticationEnums.AUTHENTICATED.getStatus();
    }

    // 实名认证接口,通过姓名和身份证号来验证
    public boolean RealNameAuthentication(User user) {
        // 通过以下代码创建API请求并设置参数。
        Id2MetaVerifyRequest request = new Id2MetaVerifyRequest();
        request.setParamType("normal");
        request.setUserName(user.getRealName());
        request.setIdentifyNum(user.getIdCardNumber());
        // 推荐，支持服务路由。
        Id2MetaVerifyResponse response = id2MetaVerifyAutoRoute(request);
        // 如果验证成功,则修改相关数据表里的信息
        if ("200".equals(response.getBody().getCode())) {
            UpdateWrapper<RealNameAuthentication> updateWrapper = new UpdateWrapper<>();
            updateWrapper.eq("user_id", user.getId()).set("realNameStatus", 1);
            realNameAuthenticationMapper.update(null, updateWrapper);
            return true;
        }
        return false;
    }
}
